/*
Copyright (c) 2008, Simon Lebettre
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the rpginventory nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.rpginventory.game;

import java.util.HashMap;

import com.jme.bounding.BoundingBox;
import com.jme.input.ChaseCamera;
import com.jme.input.InputHandler;
import com.jme.input.ThirdPersonHandler;
import com.jme.input.thirdperson.ThirdPersonMouseLook;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.scene.Node;
import com.jmex.game.StandardGame;
import com.jmex.game.state.DebugGameState;
import com.jmex.terrain.TerrainPage;

public class MyGameState extends DebugGameState {

	protected Camera camera;
	private StandardGame myGame;
	private PlayerActor player;

	private ChaseCamera chaseCamera;

	private TerrainPage terrainPage;
	
	public MyGameState(StandardGame game) {
		super(false);
		myGame = game;

		/*
		 * input = new
		 * KeyboardLookHandler(DisplaySystem.getDisplaySystem().getRenderer().getCamera(),
		 * 15.0f, 0.5f); initKeyBindings();
		 */

		initCamera();
		Vector3f position = camera.getLocation().clone();
		position = position.addLocal(0, 60, -20);
		initPlayer(position);

		chaseCamera = setupChaseCamera();
		setupInput();

	}

	private void initPlayer(Vector3f position) {
		// spawn player in front of camera
		player = new PlayerActor();
		Node playerNode = player.getPlayerNode();

		playerNode.setLocalTranslation(position);
		playerNode.updateWorldBound();
		this.getRootNode().attachChild(playerNode);

	}

	protected void initCamera() {

		camera = myGame.getCamera();

		camera.setLocation(new Vector3f(152.47104f, 7.6162586f, 145.64954f));

		camera.update();

	}

	private ChaseCamera setupChaseCamera() {

		Vector3f targetOffset = new Vector3f();

		targetOffset.y = ((BoundingBox) player.getPlayerNode().getWorldBound()).yExtent * 1.5f;

		HashMap<String, Object> chaserProps = new HashMap<String, Object>();
		chaserProps.put(ThirdPersonMouseLook.PROP_MOUSEBUTTON_FOR_LOOKING, 1);
		ChaseCamera chaser = new ChaseCamera(camera, player.getPlayerNode(), chaserProps);
		chaser.setTargetOffset(targetOffset);
		return chaser;

	}

	private void setupInput() {
		HashMap<String, Object> handlerProps = new HashMap<String, Object>();
		handlerProps.put(ThirdPersonHandler.PROP_DOGRADUAL, "true");
		handlerProps.put(ThirdPersonHandler.PROP_TURNSPEED, "" + (1.0f * FastMath.PI));
		handlerProps.put(ThirdPersonHandler.PROP_LOCKBACKWARDS, "false");
		handlerProps.put(ThirdPersonHandler.PROP_CAMERAALIGNEDMOVE, "true");

		input = new ThirdPersonHandler(player.getPlayerNode(), camera, handlerProps);
		input.setActionSpeed(100f);
	}

	public InputHandler getInput() {
		return this.input;
	}

	public void setMyGame(StandardGame myGame) {
		this.myGame = myGame;
	}

	@Override
	public void update(float tpf) {
		super.update(tpf);
		chaseCamera.update(tpf);
		if (terrainPage!=null) updateAlignTerrain();
	}
	
	
	private void updateAlignTerrain(){
	  float camMinHeight = terrainPage.getHeight(camera.getLocation()) + 2f;
      if (!Float.isInfinite(camMinHeight) && !Float.isNaN(camMinHeight)
              && camera.getLocation().y <= camMinHeight) {
          camera.getLocation().y = camMinHeight;
          camera.update();
      }

      float characterMinHeight = terrainPage.getHeight(player.getPlayerNode()
              .getLocalTranslation())+((BoundingBox)player.getPlayerNode().getWorldBound()).yExtent;
      if (!Float.isInfinite(characterMinHeight) && !Float.isNaN(characterMinHeight)) {
    	  player.getPlayerNode().getLocalTranslation().y = characterMinHeight;
      }
	}

	public Camera getCamera() {
		return camera;
	}

	public void setCamera(Camera camera) {
		this.camera = camera;
	}

	public void quit() {
		myGame.finish();
	}

	public PlayerActor getPlayer() {
		return player;
	}

	public void setPlayer(PlayerActor player) {
		this.player = player;
	}

	public TerrainPage getTerrainPage() {
		return terrainPage;
	}

	public void setTerrainPage(TerrainPage terrainPage) {
		this.terrainPage = terrainPage;
	}

}
